package com.usts2022.classcostrecord.Utils;

import android.util.Log;

import java.io.File;
import java.io.IOException;

import okhttp3.Callback;
import okhttp3.Dispatcher;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class HttpUtils {

    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private static final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");

    static Dispatcher dispatcher = new Dispatcher();
    static {
        //自定义上限
        dispatcher.setMaxRequests(3000000);
        //自定义上限
        dispatcher.setMaxRequestsPerHost(1000000);
    }

    private static OkHttpClient client = new OkHttpClient.Builder().dispatcher(dispatcher).build();

    public static void post(String url, String json,Callback callback) throws IOException {
        url = "http://192.168.10.1:8080"+ url;
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    //上传文件
    public static void imgPost(String url, File itemPhotoFile,File shoppingReceiptsFile,
                               String date,String cost,String acceptancePerson,
                               Integer ownerClass,Callback callback){
        url = "http://192.168.10.1:8080"+ url;

        RequestBody itemPhotoBody = RequestBody.create(MEDIA_TYPE_JPEG, itemPhotoFile);
        RequestBody srBody = RequestBody.create(MEDIA_TYPE_JPEG, shoppingReceiptsFile);
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("itemPhoto", "itemPhoto.jpg", itemPhotoBody)
                .addFormDataPart("shoppingReceipts", "accPerson.jpg", srBody)
                .addFormDataPart("date", date)
                .addFormDataPart("cost", cost)
                .addFormDataPart("acceptancePerson", acceptancePerson)
                .addFormDataPart("ownerClass", String.valueOf(ownerClass))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void get(String url,Callback callback) throws IOException {
        url = "http://192.168.10.1:8080"+ url;

        Request request = new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static Response getNoTh(String url) throws IOException {
        url = "http://192.168.10.1:8080"+ url;

        Request request = new Request.Builder()
                .url(url)
                .build();
        Response execute = client.newCall(request).execute();
        return execute;
    }

    //获取图片
    public static byte[] getImg(String url) throws IOException {
        url = "http://192.168.10.1:8080"+ url;

        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        byte[] img_bt = response.body().bytes();
        return img_bt;
    }

}
